home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / langs / iconv8_l.arc / IDOL.ARC / BUILTINS.IOL < prev    next >
Encoding:
Text File  |  1990-03-19  |  3.7 KB  |  152 lines

  1. # %W% %G%
  2. #
  3. # Builtin Icon objects, roughly corresponding to the language builtins.
  4. #
  5. # Taxonomy of builtin types:
  6. #
  7. #                   __Object___
  8. #                           _-'           `-_
  9. #             _-'                  `-_
  10. #          Collection            Atom_
  11. #         /    |    \                      _'     `-.
  12. #    Stack    Queue    Vector           _-'        Number
  13. #           \   /      /  |  \          _-'            /      \ 
  14. #        Deque     /      |   \          _'     Integer           Real
  15. #               \    /    |    \     /
  16. #        List    Table    String
  17. #
  18. #    
  19.  
  20. #
  21. # this is the Smalltalk-style ideal root of an inheritance hierarchy.
  22. # add your favorite methods here.
  23. #
  24. class Object()
  25.   method class()
  26.     return image(self)[8:find("_",image(self))]
  27.   end
  28. end
  29.  
  30. class Collection : Object (theCollection)
  31.   method size()
  32.     return *self.theCollection
  33.   end
  34.   method foreach()
  35.     suspend !self.theCollection
  36.   end
  37.   method random()
  38.     return ?self.theCollection
  39.   end
  40. end
  41.  
  42. class Vector : Collection()
  43.   method getElement(i)
  44.     return self.theCollection[i]
  45.   end
  46.   method setElement(i,v)
  47.     return self.theCollection[i] := v
  48.   end
  49. end
  50.  
  51. class Table : Vector(initialvalue,theCollection)
  52. initially
  53.   self.theCollection := table(self.initialvalue)
  54. end
  55.  
  56. #
  57. # The field theCollection is explicitly named so that subclasses of Stack
  58. # and Queue use these automatic initializations.  The / operator is used
  59. # to reduce the number of throw-away list allocations for subclasses which
  60. # >don't< inherit theCollection from Stack or Queue (e.g. class List).
  61. # It also allows initialization by constructor.  If one wanted to
  62. # guarantee that all Stacks start out empty but still allow class List
  63. # to be explicitly intitialized, one could remove the / here, and name
  64. # theCollection in class List, causing its initially section to override
  65. # the superclass with respect to the field theCollection.  I choose here
  66. # to maximize code sharing rather than protecting my Stack class.
  67. #
  68. # When allowing initialization by constructor one might consider
  69. # checking the type of the input to guarantee it conforms to the
  70. # type expected by the class.
  71. #
  72. class Stack : Collection(theCollection)
  73.   method push(value)
  74.     push(self.theCollection,value)
  75.   end
  76.   method pop()
  77.     return pop(self.theCollection)
  78.   end
  79. initially
  80.   /self.theCollection := []
  81. end
  82.  
  83. class Queue : Collection(theCollection)
  84.   method get()
  85.     return get(self.theCollection)
  86.   end
  87.   method put(value)
  88.     put(self.theCollection,value)
  89.   end
  90. initially
  91.   /self.theCollection := []
  92. end
  93.  
  94. class Deque : Queue : Stack()
  95. end
  96.  
  97. #
  98. # List inherits Queue's theCollection initialization, because Queue is the
  99. # first class on List's (transitively closed) superclass list to name
  100. # theCollection explicitly
  101. #
  102. class List : Deque : Vector()
  103.   method concat(l)
  104.     return List(self.theCollection ||| l)
  105.   end
  106. end
  107.  
  108. class Atom : Object(public val)
  109.   method asString()
  110.     return string(self.val)
  111.   end
  112.   method asInteger()
  113.     return integer(self.val)
  114.   end
  115.   method asReal()
  116.     return real(self.val)
  117.   end
  118. end
  119.  
  120. class Number : Atom ()
  121.   method plus(n)
  122.     return self.val + n$val()
  123.   end
  124.   method minus(n)
  125.     return self.val - n$val()
  126.   end
  127.   method times(n)
  128.     return self.val * n$val()
  129.   end
  130.   method divide(n)
  131.     return self.val / n$val()
  132.   end
  133. end
  134.  
  135. class Integer : Number()
  136. initially
  137.   if not (self.val := integer(self.val)) then
  138.     stop("can't make Integer from ",image(self.val))
  139. end
  140.  
  141. class Real : Number()
  142. initially
  143.   if not (self.val := real(self.val)) then
  144.     stop("can't make Real from ",image(self.val))
  145. end
  146.  
  147. class String : Vector : Atom()
  148.   method concat(s)
  149.     return self.theCollection || s
  150.   end
  151. end
  152.